package io.github.talelin.latticy.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.github.talelin.latticy.bo.rule.ChargeAbsoluteBO;
import io.github.talelin.latticy.common.constant.RuleTemplateConstant;
import io.github.talelin.latticy.model.InboundPackagesDO;
import io.github.talelin.latticy.model.RulesDO;
import io.github.talelin.latticy.mapper.RulesMapper;
import io.github.talelin.latticy.service.ChargeAbsoluteService;
import io.github.talelin.latticy.service.RulesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@wulongbo
 * @since 2022-08-06
 */
@Service
public class RulesServiceImpl extends ServiceImpl<RulesMapper, RulesDO> implements RulesService {

    @Autowired
    private ChargeAbsoluteService chargeAbsoluteService;

    @Override
    public List<RulesDO> getRulesByUserId(int userId) {
        QueryWrapper<RulesDO> wrapper = new QueryWrapper<>();
        return this.list(wrapper);
    }

    @Override
    public List<RulesDO> getAllRules() {
        List<RulesDO> list = this.baseMapper.getAllRules();
        List<ChargeAbsoluteBO> absoluteBOS = chargeAbsoluteService.getAllChargeAbsolute();
        Map<Integer, List<ChargeAbsoluteBO>> absoluteMap = absoluteBOS.parallelStream().collect(Collectors.groupingBy(ChargeAbsoluteBO::getRuleId));
        list.parallelStream().forEach(
                rulesDO -> {
                    if (rulesDO.getRule().contains(RuleTemplateConstant.PLACEHOLDER)) {
                        List<ChargeAbsoluteBO> currentList = absoluteMap.get(rulesDO.getId());
                        StringBuffer sb = new StringBuffer(RuleTemplateConstant.RULE_HEAD);
                        currentList.stream().forEach(
                                chargeAbsoluteBO -> {
                                    StringBuffer subSB = new StringBuffer();
                                    subSB.append(RuleTemplateConstant.RULE_BODY)
                                            .append(chargeAbsoluteBO.getFtlKey()).append(RuleTemplateConstant.METHOD_BODY)
                                            .append(RuleTemplateConstant.CONNECT_CHAR)
                                            .append(RuleTemplateConstant.STRING_CHAR)
                                            .append(chargeAbsoluteBO.getRuleValueId())
                                            .append(RuleTemplateConstant.STRING_CHAR)
                                            .append(RuleTemplateConstant.CONNECT_CHAR)
                                            .append(RuleTemplateConstant.STRING_CHAR)
                                            .append(chargeAbsoluteBO.getMin())
                                            .append(RuleTemplateConstant.STRING_CHAR)
                                            .append(RuleTemplateConstant.CONNECT_CHAR)
                                            .append(RuleTemplateConstant.STRING_CHAR)
                                            .append(chargeAbsoluteBO.getMax())
                                            .append(RuleTemplateConstant.STRING_CHAR)
                                            .append(RuleTemplateConstant.CONNECT_CHAR)
                                            .append(chargeAbsoluteBO.getAccuracy()).append(RuleTemplateConstant.RULE_FOOT);
                                    sb.append(subSB);
                                }
                        );
                        rulesDO.setRule(rulesDO.getRule().replace(RuleTemplateConstant.PLACEHOLDER, sb.toString()));
                    }
                }
        );
        return list;
    }

    //图书优惠计算
    @Override
    public void calculate(String type) {

    }

    @Override
    public boolean checkRuleExistByName(String name) {
        QueryWrapper<RulesDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RulesDO::getName, name);
        return this.count(wrapper) > 0;
    }

    @Override
    public boolean updateRuleNameById(Integer ruleId, String name) {
        Assert.isFalse(checkRuleExistByName(name), "18802");
        return this.updateById(new RulesDO(ruleId, name));
    }

    @Override
    public boolean updateBaseCostById(Integer ruleId, BigDecimal baseCost) {
        return this.updateById(new RulesDO(ruleId, baseCost));
    }

    @Override
    public boolean deleteRulesByChargeItemId(Integer chargeItemId) {
        return this.baseMapper.deleteRulesByChargeItemId(chargeItemId) > 0;
    }

}
